package com.eric.study.reactor.controller;

import com.eric.study.reactor.commons.response.Response;
import com.eric.study.reactor.entity.User;
import com.eric.study.reactor.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * <p> UserController.
 *
 * <p>
 * change history:
 *
 * <pre>
 * date         defect       person          comments
 * --------------------------------------------------------------------------------
 * 2023/9/20    ********     Qiheng Zhang    create file.
 * </pre>
 *
 * @author Qiheng Zhang
 * @date 2023/9/20 15:36
 * @copyright 2023, FA Software (Shanghai) Co., Ltd. All Rights Reserved.
 * @since 1.0.0
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/get/{id}")
    public Mono<Response> getUser(@PathVariable int id) {
        log.info("获取指定用户：{}", id);
        return userService.getUserById(id).map(Response::success);
    }

    @PostMapping("/all")
    public Mono<Response> allUsers() {
        log.info("获取所有用户");
        return userService.allUser().collectList().map(Response::success);
    }

    @PostMapping("/add")
    public Mono<Response> saveUser(@RequestBody @Validated Mono<User> user) {
        log.info("新增用户：{}", user);
        return userService.addUser(user).map(Response::success);
    }

    @PostMapping("/add_2")
    public Mono<Response> saveUser(@RequestBody @Validated User user) {
        log.info("新增用户：{}", user);
        return userService.addUser(Mono.just(user)).map(Response::success);
    }

}
